package controllers
{
	import flash.events.NetStatusEvent;
	import flash.external.ExternalInterface;
	import flash.net.NetStream;
	
	import models.LocalSetting;
	import models.StratusConnection;
	import models.vo.UserVO;

	public class VideoController
	{
		public var inStreams:Object = {};
		public var outStream:NetStream;
		private var netStreamReady:Boolean;
		[DexterBinding]
		public var localSetting:LocalSetting;
		[DexterBinding]
		public var stratus:StratusConnection;
		[DexterEvent]
		public function stratusInfo(event:NetStatusEvent):void{
			switch(event.info.code){
				case "NetConnection.Connect.Success":
//					inStream = new NetStream(stratus,stratus.groupSpecifier.groupspecWithAuthorizations());
//					inStream.client = this;
//					inStream.addEventListener(NetStatusEvent.NET_STATUS,stratusInfo);
					break;
				case "NetStream.Connect.Success":
					netStreamReady = true;
					if(event.info.stream == outStream){
						publish();
					}
					break;
			}
		}
		[DexterEvent]
		public function playStream(id:String):void{
			var inStream:NetStream = getStream(id);
			if(!inStream)inStream = new NetStream(stratus,stratus.groupSpecifier.groupspecWithAuthorizations());
			inStream.bufferTime = localSetting.bufferTime;
			inStream.receiveAudio(true);
			inStreams[id] = inStream;
			inStream.play(id);
		}
		[DexterEvent]
		public function stopPlay(id:String = null):void{
			if(!id){
				for each(var inStream:NetStream in inStreams){
					inStream.close();
				}
				inStreams = {};
			}else{
				inStreams[id].close();
				delete inStreams[id];
			}
		}
		[DexterEvent]
		public function getStream(id:String):NetStream{
			return inStreams[id];
		}
		[DexterEvent]
		public function changeBufferTime(bf:Number):void{
			for each(var stream:NetStream in inStreams){
				stream.bufferTime = bf;
			}
		}
		[DexterEvent]
		public function publishStream(useCam:Boolean = true):void{
			if(!outStream){
				outStream = new NetStream(stratus,stratus.groupSpecifier.groupspecWithAuthorizations());
				outStream.client = this;
				outStream.addEventListener(NetStatusEvent.NET_STATUS,stratusInfo);
				if(localSetting.cameraSetting){
					localSetting.cam.setMode(localSetting.cameraSetting.width,localSetting.cameraSetting.height,localSetting.cameraSetting.fps);
					localSetting.cam.setQuality(localSetting.cameraSetting.bandwidth,localSetting.cameraSetting.quality);
					localSetting.cam.setKeyFrameInterval(localSetting.cameraSetting.keyFarmeInterval);
				}else{
					localSetting.cam.setMode(320,240,15);
				}
				outStream.attachAudio(localSetting.mic);
			}
			outStream.attachCamera(useCam?localSetting.cam:null);
			if(netStreamReady){
				publish();
			}
		}
		[DexterEvent]
		public function changeDevice():void{
			if(outStream){
				outStream.attachCamera(localSetting.cam);
				outStream.attachAudio(localSetting.mic);
			}
		}
		private function publish():void{
			outStream.publish(UserVO.self.id);
		}
	}
}